package com.web.text.html;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlStrUtil {

    private HtmlStrUtil() {
    }

    public static String cleanHtmlTag(String content) {
        return content.replaceAll("(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)", "");
    }

    public static String getTagName(String html) {
        Pattern compile = Pattern.compile("^<[/]?([a-zA-Z]+)\\s*?[^>]*?/?>$");
        Matcher matcher = compile.matcher(html);
        if (matcher.matches() && matcher.groupCount() >= 1) {
            return matcher.group(1);
        }
        return null;
    }

    public static List<int[]> findTag(String content) {
        final List<int[]> list = new ArrayList<>();
        findTag(content, item -> {
            list.add(item);
        });
        return list;
    }


    public static void findTag(String content, Callback<int[]> callback) {
        Pattern compile = Pattern.compile("(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)");
        Matcher matcher = compile.matcher(content);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            callback.call(new int[]{start, end});
        }
    }

    public static List<HtmlItem> parseHtmlTree(String content) {
        int i = 0;
        List<HtmlItem> list = new ArrayList<>();
        findTag(content, new Callback<int[]>() {
            int i = 0;
            HtmlItem parent;

            @Override
            public void call(int[] item) {
                if (i < item[0]) {
                    String sub = content.substring(i, item[0]);
                    if (null != parent) {
                        parent.add(new HtmlItem(sub, false));
                    } else {
                        list.add(new HtmlItem(sub, false));
                    }
                }
                String sub = content.substring(item[0], item[1]);
                HtmlItem htmlItem = new HtmlItem(sub, true);
                if (null == parent) {
                    parent = htmlItem;
                    list.add(htmlItem);
                } else if (null != parent && htmlItem.getTagName().equals(parent.getTagName()) && !htmlItem.isStart()) {
                    htmlItem.setParent(parent);
                    parent.add(htmlItem);
                    parent = parent.getParent();
                } else {
                    parent.add(htmlItem);
                }
                i = item[1];
            }
        });
        return list;
    }


    /**
     * 清除成对空标签
     *
     * @param content
     * @return
     */
    public static String cleanPairEmptyTag(String content) {
        return content;
    }


    /**
     * 清除指定标签，并且在多少个非标签字符之后
     *
     * @param content
     * @return
     */
    public static String cleanTagByName(String content, boolean withTagContent, String... tagNames) {
        String[] var4 = tagNames;
        int var5 = tagNames.length;
        for (int var6 = 0; var6 < var5; ++var6) {
            String tagName = var4[var6];
            if (null != tagName && tagName.length() > 0) {
                tagName = tagName.trim();
                String regex;
                if (withTagContent) {
                    regex = String.format("(?i)<%s\\s*?[^>]*?/?>(.*?</%s>)?", tagName, tagName);
                } else {
                    regex = String.format("(?i)<%s\\s*?[^>]*?>|</%s>", tagName, tagName);
                }
                content = content.replaceAll(regex, "");
            }
        }
        return content;
    }


    private interface Callback<T> {
        void call(T t);
    }
}
